package com.hyf.treeview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hyf.treeview.tree.Node;
import com.hyf.treeview.tree.TreeListViewAdapter;

import java.util.List;

/**
 * Created by Administrator on 2018/1/3.
 */

public class SimpleBaseAdapter<T> extends TreeListViewAdapter {
    public SimpleBaseAdapter(Context context, ListView listView, List<T> datas, int defaultLevel) throws Exception {
        super(context, listView, datas, defaultLevel);
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.layout_tree_node_item, parent, false);
            holder = new ViewHolder();
            holder.mIcon = convertView.findViewById(R.id.iv_tree_node_item);
            holder.mTextView = convertView.findViewById(R.id.tv_tree_node_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (node.getIcon() == -1) {
            holder.mIcon.setVisibility(View.INVISIBLE);
        } else {
            holder.mIcon.setVisibility(View.VISIBLE);
            holder.mIcon.setImageResource(node.getIcon());
        }
        holder.mTextView.setText(node.getName());

        return convertView;
    }

    class ViewHolder {
        ImageView mIcon;
        TextView mTextView;
    }
}
